package es.cazadoresmadridejos.beans.controller.biggame;

import java.io.OutputStream;
import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.displaytag.pagination.PaginatedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import es.cazadoresmadridejos.beans.manager.biggame.BigGameManager;
import es.cazadoresmadridejos.beans.manager.session.UserPreferences;
import es.cazadoresmadridejos.model.BigGame;
import es.cazadoresmadridejos.objs.web.Page;
import es.cazadoresmadridejos.util.itext.ITextProcessor;

@Controller
public class BigGameListController {
	
	@Autowired private BigGameManager bigGameManager;
	@Autowired(required=false) private UserPreferences userPreferences;
	@Autowired private ITextProcessor iTextProcessor;

	@RequestMapping(value="/bigGameList*.html")
	public ModelAndView list(
			HttpServletRequest request,
			Boolean isList
			) throws Throwable {

		ModelAndView mv;
		if(Boolean.TRUE.equals(isList)){
			mv = new ModelAndView("bigGameList");
		}else{
			mv = new ModelAndView();
		}		

		PaginatedList list = bigGameManager.findAllPaginated(new Page(request),userPreferences.getReason().getYear());

		mv.addObject("list", list);
		return mv;
	}


	@RequestMapping(value="/bigGameDelete.html")
	public ModelAndView delete(
			@RequestParam(value="id")Long id,
			HttpServletRequest request
			) throws Throwable {

		bigGameManager.delete(id);

		return list(request,true);
	}
	
	@RequestMapping(value="/bigGameDownload.html")
	public void download(
			@RequestParam(value="id")Long id,
			HttpServletRequest request,
			HttpServletResponse response
			) throws Throwable {
		
		BigGame bigGame = bigGameManager.get(id);
		OutputStream out = response.getOutputStream();
		
		response.setContentType( "application/x-download" );
		response.setHeader( "Content-Disposition", "attachment; filename=monteria.pdf");
		BigDecimal amount =bigGame.getAmount();
		if(amount == null){
			amount = BigDecimal.ZERO;
		}
		amount.setScale(0);
		iTextProcessor.create(out, bigGame.getAssistances(), amount.toString(), bigGame.getRaffleHour(), bigGame.getArrivalHour(), bigGame.getDecoratedDate());
////		
////		InputStream is = getClass().getClassLoader().getResourceAsStream("templates/biggame.jrxml");
//////		FileInputStream fis = new FileInputStream(new File("classpath:templates/biggame.jrxml"));
////		JasperReport jasperReport = JasperCompileManager.compileReport(is);
//		
////		Map<String, Object> params = new HashMap<String, Object>();
////		Document document = JRXmlUtils.parse(XmlUtils.bigGameXml(bigGame));
//		
////		
////		params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "dd-MM-yyyy");
////		params.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##");
////		params.put(JRXPathQueryExecuterFactory.XML_LOCALE, request.getLocale());
////		params.put(JRParameter.REPORT_LOCALE, request.getLocale());	    
////		
////		JasperPrint jasperPrint = JasperFillManager.fillReport(
////				getClass().getClassLoader().getResourceAsStream("templates/biggame.jasper") , 
////				params,
////				new JRXmlDataSource(document));
////		
////		JasperExportManager.exportReportToPdfStream(jasperPrint,out);
//		
//		Document document = JRXmlUtils.parse(XmlUtils.bigGameXml(bigGame));
//		
//		String jasperTemplate = "biggame.jasper";
//		if(bigGame.getStands() > 52 && bigGame.getStands() < 60){
//			jasperTemplate = "biggame52_59.jasper";
//		}else if(bigGame.getStands() > 59 && bigGame.getStands() < 69){
//			jasperTemplate = "biggame60_68.jasper";
//		}if(bigGame.getStands() > 68){
//			jasperTemplate = "biggame68_77.jasper";
//		}
//		
//		JasperReport jasperReport = null;
//		jasperReport = (JasperReport) JRLoader.loadObject(Thread.currentThread().getContextClassLoader().getResourceAsStream("templates/" + jasperTemplate));
//		Map<String, Object> params = new HashMap<String, Object>();
//		params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT,document);
//		params.put(JRXPathQueryExecuterFactory.XML_DATE_PATTERN, "yyyy-MM-dd");
//		params.put(JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN, "#,##0.##");
//		params.put(JRXPathQueryExecuterFactory.XML_LOCALE, Locale.ENGLISH);
//		params.put(JRParameter.REPORT_LOCALE, request.getLocale());
//		
//		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,params);
//		JasperExportManager.exportReportToPdfStream(jasperPrint, out);		
		
		out.flush();
		out.close();		
	}

}